"""Fix foreign key constraints for data source logs

Revision ID: e878809dde0a
Revises: c62c08df4365
Create Date: 2025-09-12 00:02:05.046268

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'e878809dde0a'
down_revision: Union[str, Sequence[str], None] = 'c62c08df4365'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    # 只修改外键约束，添加级联删除
    op.drop_constraint(None, 'data_source_preview_log', type_='foreignkey')
    op.create_foreign_key(None, 'data_source_preview_log', 'data_source', ['data_source_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'data_source_sync_log', type_='foreignkey')
    op.create_foreign_key(None, 'data_source_sync_log', 'data_source', ['data_source_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'database_config', type_='foreignkey')
    op.create_foreign_key(None, 'database_config', 'data_source', ['data_source_id'], ['id'], ondelete='CASCADE')
    op.drop_constraint(None, 'excel_config', type_='foreignkey')
    op.create_foreign_key(None, 'excel_config', 'data_source', ['data_source_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    # 恢复原始外键约束，移除级联删除
    op.drop_constraint(None, 'excel_config', type_='foreignkey')
    op.create_foreign_key(None, 'excel_config', 'data_source', ['data_source_id'], ['id'])
    op.drop_constraint(None, 'database_config', type_='foreignkey')
    op.create_foreign_key(None, 'database_config', 'data_source', ['data_source_id'], ['id'])
    op.drop_constraint(None, 'data_source_sync_log', type_='foreignkey')
    op.create_foreign_key(None, 'data_source_sync_log', 'data_source', ['data_source_id'], ['id'])
    op.drop_constraint(None, 'data_source_preview_log', type_='foreignkey')
    op.create_foreign_key(None, 'data_source_preview_log', 'data_source', ['data_source_id'], ['id'])
    # ### end Alembic commands ###
